/**
 * Copyright (c) 2020 kedacom
 * OpenATC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 **/
<template>
  <div class="custom-cross-part">
    <FirstImportCrossmap v-if="pageindex === 1"
    @loadSvgString="loadSvgString" />
    <CustomDraw v-show="pageindex === 2" ref="CustomDraw"
      :loadedChannelizatonData="loadedChannelizatonData" />
  </div>
</template>
<script>
import FirstImportCrossmap from './firstImportCrossmap'
import CustomDraw from './index.draw'
import { mapState } from 'vuex'
import { getIframdevid } from '@/utils/auth'
import { getChannelizatonChart } from '@/api/cross'
// import { getMessageByCode } from '../../../utils/responseMessage.js'

export default {
  name: 'custom-cross-part',
  components: {
    FirstImportCrossmap,
    CustomDraw
  },
  data () {
    return {
      pageindex: 1,
      loadedChannelizatonData: {} // 接口返回的已保存路口渠化数据
    }
  },
  computed: {
    ...mapState({
      channelizationPageIndex: state => state.globalParam.channelizationPageIndex,
      curOpenatcAgentid: state => state.globalParam.curOpenatcAgentid
    })
  },
  watch: {
    channelizationPageIndex: {
      handler: function (index) {
        this.pageindex = index
      }
    },
    curOpenatcAgentid: {
      handler: function (val) {
        this.getChannelizatonChart()
      },
      deep: true
    }
  },
  methods: {
    loadSvgString (type, imgstr) {
      this.pageindex = 2
      this.$nextTick(() => {
        this.$refs.CustomDraw.loadSvgString(type, imgstr)
      })
    },
    getChannelizatonChart () {
      let agentid = getIframdevid()
      let _this = this
      _this.$store.dispatch('SetChannelizationPageIndex', false)
      getChannelizatonChart(agentid).then(data => {
        if (!data.data.success) {
          // let parrenterror = getMessageByCode(data.data.code, _this.$i18n.locale)
          // if (data.data.data) {
          //   // 子类型错误
          //   let childErrorCode = data.data.data.errorCode
          //   if (childErrorCode) {
          //     let childerror = getMessageByCode(data.data.data.errorCode, _this.$i18n.locale)
          //     _this.$message.error(parrenterror + ',' + childerror)
          //   }
          // } else {
          //   _this.$message.error(parrenterror)
          // }
          return
        }
        if (JSON.stringify(data.data.data) === '{}') return
        _this.$store.dispatch('SetChannelizationPageIndex', true)
        _this.loadedChannelizatonData = JSON.parse(JSON.stringify(data.data.data))
      })
    }
  },
  mounted () {
    this.getChannelizatonChart()
  }
}
</script>
<style scoped>
</style>
